using System;
using NUnit.Framework;
using ValidationFramework.Configuration;
using ValidationFramework.Extensions;
using ValidationFramework.Reflection;

namespace ValidationFramework.Tests.Configuration
{
    [TestFixture]
    public class CompareStructRuleConfigReaderTest
    {
        #region Methods

        [Test]
        public void ReadConfig()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='CompareRule' ruleSet='foo'  valueToCompare='1' compareOperator='Equal'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
			var rule = (CompareRule<int>)ConfigurationService.GetRule(ruleData, TypePointers.IntTypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.AreEqual(1, rule.ValueToCompare);
            Assert.AreEqual(CompareOperator.Equal, rule.CompareOperator);
        }


        [Test]
        public void ReadConfigNullable()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='CompareRule' ruleSet='foo'  valueToCompare='1' compareOperator='Equal'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (CompareRule<int>)ConfigurationService.GetRule(ruleData, typeof(int?).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.AreEqual(1, rule.ValueToCompare);
            Assert.AreEqual(CompareOperator.Equal, rule.CompareOperator);
        }

        [Test]
        public void ReadDateTimeConfig()
        {
            const string validatorXml = "<rule errorMessage='hello' valueToCompare='10 Mar 2007' typeName='CompareRule' ruleSet='foo'   compareOperator='Equal'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (CompareRule<DateTime>)ConfigurationService.GetRule(ruleData, typeof(DateTime).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.AreEqual(new DateTime(2007,3,10), rule.ValueToCompare);
            Assert.AreEqual(CompareOperator.Equal, rule.CompareOperator);
        }


        [Test]
        public void ReadDateTimeConfigNullable()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='CompareRule' ruleSet='foo' valueToCompare='10 Mar 2007' compareOperator='Equal'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (CompareRule<DateTime>)ConfigurationService.GetRule(ruleData, typeof(DateTime?).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.AreEqual(new DateTime(2007, 3, 10), rule.ValueToCompare);
            Assert.AreEqual(CompareOperator.Equal, rule.CompareOperator);
        }

        #endregion


    }
}